<template>
  <div class="app-container">
    <el-form ref="filterForm" :inline="true" class="filter-container">
      <el-form-item>
        <el-button
          size="small"
          type="success"
          icon="el-icon-plus"
          @click="toMenuNew"
        >添加菜单</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      row-key="id"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
      :tree-props="{
        children: 'sub_button',
        hasChildren: 'hasChildren'
      }"
    >
      <el-table-column
        prop="name"
        label="菜单名称"
        width="240"
      />
      <el-table-column
        prop="id"
        label="ID"
        width="80"
        align="center"
      />
      <el-table-column
        prop="url"
        label="菜单链接"
      />
      <el-table-column
        prop="settime"
        label="创建时间"
        width="160"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.settime | moment('YYYY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="toMenuEdit(scope.row.id)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getMenuList } from '@/api/menu'

export default {
  name: 'MenuSettingList',
  data() {
    return {
      list: [],
      listLoading: true,
      total: 0
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getMenuList().then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    toMenuNew() {
      this.$router.push('/setting/menu/new')
    },
    toMenuEdit(id) {
      this.$router.push(`/setting/menu/edit/${id}`)
    }
  }
}
</script>
